﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.Collections.Generic;
using XCI.Core;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 服务基类
    /// </summary>
    public class BaseService<T> where T : class
    {
        /// <summary>
        /// 根地址
        /// </summary>
        protected virtual string RootUrl { get; }

        /// <summary>
        /// 获取接口地址
        /// </summary>
        /// <param name="name">接口名称</param>
        /// <param name="paths">路径参数</param>
        protected string GetUrl(string name, params string[] paths)
        {
            var rurl = RootUrl;
            if (!rurl.EndsWith("/"))
            {
                rurl = rurl + "/";
            }
            if (name.StartsWith("/"))
            {
                name = name.Substring(1);
            }
            var url = string.Concat(rurl, name);
            if (paths != null && paths.Length > 0)
            {
                foreach (var path in paths)
                {
                    if (string.IsNullOrEmpty(path)) continue;
                    url = string.Concat(url, "/", path);
                }
            }
            return url;
        }

        /// <summary>
        /// 转为分页Map
        /// </summary>
        /// <param name="pageParam">分页参数</param>
        /// <param name="sortConvert">排序字段名称转换器</param>
        protected Map ToPageMap(PageParam pageParam, IDictionary<string, string> sortConvert = null)
        {
            Map map = new Map();
            if (pageParam.PageIndex.HasValue)
            {
                map.Add("pageIndex", pageParam.PageIndex.Value.ToString());
            }
            if (pageParam.PageSize.HasValue)
            {
                map.Add("pageSize", pageParam.PageSize.Value.ToString());
            }
            if (!string.IsNullOrEmpty(pageParam.SortName) && !string.IsNullOrEmpty(pageParam.SortDir))
            {
                var sname = pageParam.SortName;
                if (sortConvert != null
                    && sortConvert.Count > 0
                    && sortConvert.TryGetValue(pageParam.SortName, out string mapName))
                {
                    sname = mapName;
                }
                map.Add("sortName", sname);
                map.Add("sortDir", pageParam.SortDir);
            }
            return map;
        }

        /// <summary>
        /// 创建新Id
        /// </summary>
        protected string NewId()
        {
            return SysService.Instance.GetId();
        }

        /// <summary>
        /// 检查主键是否存在
        /// </summary>
        /// <param name="id">主键</param>
        protected bool ExistByIdCore(string id)
        {
            var url = GetUrl(SysConst.ApiExistByIdName, id);
            return SysRuntime.Api.Post<bool>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 检查编码是否存在
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="id">排除的主键</param>
        protected bool ExistByCodeCore(string code, string id = null)
        {
            var url = GetUrl(SysConst.ApiExistByCodeName, code, id);
            return SysRuntime.Api.Post<bool>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 检查名称是否存在
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="id">排除的主键</param>
        protected bool ExistByNameCore(string name, string id = null)
        {
            var url = GetUrl(SysConst.ApiExistByNameName, name, id);
            return SysRuntime.Api.Post<bool>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 新建数据
        /// </summary>
        /// <param name="model">数据模型</param>
        protected bool InsertCore(T model)
        {
            var url = GetUrl(SysConst.ApiInsertName);
            return SysRuntime.Api.PostJsonAsMessage(url, model).EnsureSuccess().Success;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model">数据模型</param>
        protected bool UpdateCore(T model)
        {
            var url = GetUrl(SysConst.ApiUpdateName);
            return SysRuntime.Api.PostJsonAsMessage(url, model).EnsureSuccess().Success;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        protected bool DeleteByIdCore(string id)
        {
            var url = GetUrl(SysConst.ApiDeleteName);
            var map = new Map
            {
                { SysConst.FIdName, id }
            };
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids">主键字符串</param>
        protected bool DeleteByIdsCore(string ids)
        {
            var url = GetUrl(SysConst.ApiDeleteName);
            var map = new Map
            {
                { SysConst.FIdsName, ids }
            };
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="status">状态 true:启用 false:禁用</param>
        /// <returns>成功返回true</returns>
        protected bool UpdateStatusByIdCore(string id, bool status)
        {
            var url = GetUrl(SysConst.ApiUpdateStatusName);
            var map = new Map
            {
                { SysConst.FIdName, id },
                { SysConst.FStatusName, status.ToString() }
            };
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态 true:启用 false:禁用</param>
        /// <returns>成功返回true</returns>
        protected bool UpdateStatusByIdsCore(string ids, bool status)
        {
            var url = GetUrl(SysConst.ApiUpdateStatusName);
            var map = new Map
            {
                { SysConst.FIdsName, ids },
                { SysConst.FStatusName, status.ToString() }
            };
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 批量修改字段值
        /// </summary>
        /// <param name="keyValues">修改的数据集合</param>
        /// <returns>成功返回true</returns>
        protected bool BatchUpdateValuesCore(List<PrimaryKeyValue> keyValues)
        {
            var url = GetUrl(SysConst.ApiBatchUpdateValuesName);
            return SysRuntime.Api.PostJsonAsMessage(url, keyValues).EnsureSuccess().Success;
        }

        /// <summary>
        /// 根据主键获取数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>成功返回模型对象</returns>
        protected T SelectByIdCore(string id)
        {
            var url = GetUrl(SysConst.ApiSelectByIdName, id);
            return SysRuntime.Api.Post<T>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 根据编码获取数据
        /// </summary>
        /// <param name="code">编码</param>
        /// <returns>成功返回模型对象</returns>
        protected T SelectByCodeCore(string code)
        {
            var url = GetUrl(SysConst.ApiSelectByCodeName, code);
            return SysRuntime.Api.Post<T>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 刷新服务端缓存
        /// </summary>
        protected bool RefreshCacheCore()
        {
            var url = GetUrl(SysConst.ApiRefreshCacheName);
            return SysRuntime.Api.PostAsMessage(url).EnsureSuccess().Success;
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        protected PageList<T> SelectPageListCore(Map map = null)
        {
            var url = GetUrl(SysConst.ApiSelectPageListName);
            return SysRuntime.Api.Post<PageList<T>>(url, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        protected List<T> SelectListCore(Map map = null)
        {
            var url = GetUrl(SysConst.ApiSelectListName);
            return SysRuntime.Api.Post<List<T>>(url, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        protected byte[] ExportCore(Map map = null)
        {
            var url = GetUrl(SysConst.ApiExportName);
            return SysRuntime.Api.DownloadPost(url, map).EnsureSuccess().Data;
        }
    }
}